<?php

class Import_flickr extends Controller 
{

	function Import_flickr()
	{
		parent::Controller();
		$this->template['module'] = "album";
		$this->load->model("album_model", "album");
		if(!isset($this->album->album_settings))
		{
			echo __("This module is not yet set up.", "album")  . "<br />";
			echo anchor('admin/album/settings', __("Go here to set it up", "album"));
			exit();
		}
	}
	
	function index()
	{
		$this->import();
	}
	
	function import($aid = null)
	{
		$this->template['title'] = __("Import photos from flickr", "album") ;
		$this->user->require_login();
		if(is_null($aid))
		{
			$this->session->set_flashdata('notification', __("Album id not specified", "album"));
			redirect('album/user/' . $this->user->username);
			return;
		
		}
		
		$album = $this->album->get_album(array('where' => array('a.aid' => $aid)));
		if ($album['username'] != $this->user->username && $this->user->level['album'] < LEVEL_ADD)
		{
			$this->session->set_flashdata('notification', __("You cannot add photo into that album.", "album"));
			redirect('album/user/' . $this->user->username);
			return;
		}
		
		if($this->input->post('submit'))
		{
			if(!isset($this->album->album_settings->flickr_api_key) || $this->album->album_settings->flickr_api_key === false)
			{
				$this->session->set_flashdata('notification', __("Please set the flickr api key", "album"));
				redirect('admin/album/settings/flickr');

			}
			$num = 20;
			if (intval($this->input->post('num')) != 0) $num = intval($this->input->post('num'));
			$this->load->plugin('phpflickr');	
			$this->f = new phpFlickr($this->album->album_settings->flickr_api_key);
			$this->f->enableCache("fs","./cache");
			if(! $this->input->post('username'))
			{
			
				if ($tag = $this->input->post('tag'))
				{
					$photos = $this->f->photos_search(array('tags' => $tag, 'tag_mode' => 'all', 'per_page' => $num));
				}
				else
				{
					$this->session->set_flashdata('notification', __("You have to specify at least one of those fields", "album"));
					redirect('album/import_flickr', 'refresh');
					return;
				}
			
			
			}
			else
			{
				$person = $this->f->people_findByUsername($this->input->post('username'));
				if ($tag = $this->input->post('tag'))
				{
					$photos = $this->f->photos_search(array('user_id' => $person['id'], 'tags' => $tag, 'tag_mode' => 'all', 'per_page' => $num));
				}
				else
				{
					$photos = $this->f->people_getPublicPhotos($person['id'], NULL, $num);
				}
			
			}
			

			//$array_photo = array_reverse((array)$photos['photo']);
			$array_photo = $photos['photo'];
		
			foreach ($array_photo as $photo) {
				$data = array(
				'username' => $this->user->username,
				'email' => $this->user->email,
				'sid' => uniqid('s'),
				'aid' => $aid,
				's' => $this->f->buildPhotoURL($photo, "Square"),
				't' => $this->f->buildPhotoURL($photo, "Thumbnail"),
				'm' => $this->f->buildPhotoURL($photo, "Small"),
				'o' => $this->f->buildPhotoURL($photo, "Medium"),
				'title' => (isset($photo['title'])? $photo['title'] : ''),
				'description' => (isset($photo['description']) ? $photo['description'] : '')
				);
				
				$this->album->add_image($data);
			}
			redirect('album/ampidiro/' . $aid);
		}
		else
		{
			$this->template['album'] = $album;
			$this->layout->load($this->template, 'album/import_flickr');
		}
	}

}